﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.ZXBuiness;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Repository.ZXBuiness
{
    public static class UserFenRunRep
    {

        public static ZXUserFenRun GetById(this IRepositoryAsync<ZXUserFenRun> repository, long UserFenRunId)
        {
            Expression<Func<ZXUserFenRun, bool>> express = i => 1 == 1;

            express = express.AndAlso(i => i.UserFenRunId == UserFenRunId);

            return repository.Query().Where(express).Select().FirstOrDefault();
        }

        public static List<ZXUserFenRun> GetFenRunList(this IRepositoryAsync<ZXUserFenRun> repository, int offset, int limit, long? zxUserId,int? state)
        {
            string whereCondit = string.Format(@" where  1=1 ");
          
            if (zxUserId.HasValue)
            {
                whereCondit += string.Format(@" and zuf.ZX_UserId ={0} ", zxUserId.Value);
            }
            if (state.HasValue)
            {
                whereCondit += string.Format(@" and zuf.IsTiXian ={0} ", state.Value);
            }

            string exeTotalSql = string.Format(@"
             declare   @TOTAL INT
              SELECT  @TOTAL =count(zuf.UserFenRunId )  
              FROM ZX_UserFenRun  zuf  
                {0}
            ", whereCondit);

            string exeSql = string.Format(@"
        select * from ( 
              SELECT ROW_NUMBER() OVER(order by IsTiXian ,zuf.CreateTime desc) AS RowId, zuf.[UserFenRunId]
                  ,zuf.[ZX_UserId]
                  ,zuf.[OrderFengyunId]
                  ,zuf.[FenRunLevel]
                  ,zuf.[FenRunMoney]
                  ,zuf.[Remarks]
                  ,zuf.[CreateTime]
                  ,zuf.[TiXianTime]
                  ,zuf.[IsTiXian]
                  ,zuf.[AccountBalanceCode]  
                   ,zuf.FRcode
                  ,@TOTAL AS Total
              FROM ZX_UserFenRun  zuf  
                {0}
            )  as b
         where RowId between {1} and {2} 
            ", whereCondit, (offset-1)*limit, offset * limit);
            return repository.SqlQueryT<ZXUserFenRun>(exeTotalSql + exeSql).ToList();
        }

        public static ZXFenRunDetailModel GetFenRunOrderInfoByUFRid(this IRepositoryAsync<ZXUserFenRun> repository, long UserFenRunId) {
            string orderIdCondit = string.Format(" and ZUFR.UserFenRunId={0} ", UserFenRunId);
            string exeSql = string.Format(@"                 
                            SELECT ZUFR.FenRunLevel,ZUFR.ZX_UserId,ZUFR.UserFenRunId,ZUFR.OrderFengyunId,
                            ZUFR.FenRunMoney,ZOFR.ProductId,ZOFR.ProductName,WP.Domain+ WP.ThumbnailUrl1 AS ProductImg,WO.OrderCode,WO.CreateTime as payedTime,
                            zofr.OrderFenRunMoney AS OrderPayedMoeny,WU.NickName AS FromWho,WU.UserID AS FromWhoId
                              FROM [ZX_UserFenRun] ZUFR
                            INNER JOIN ZX_Order_FenRunInfo ZOFR
                            ON ZUFR.OrderFengyunId = ZOFR.OrderFengyunId
                            INNER JOIN WeiXin_Order WO 
                            ON WO.OrderID = ZOFR.OrderId
                            INNER JOIN WeiXin_Product WP
                            ON WP.ProductID = ZOFR.ProductId
                            INNER JOIN ZX_UserInfo ZU
                            ON ZU.ZX_UserId = ZOFR.UserId
                            INNER JOIN WeiXin_User WU
                            ON WU.UserID = ZU.WX_UserId
                              where 1=1  {0}    
               
                            ", orderIdCondit);
            return repository.SqlQueryT<ZXFenRunDetailModel>(exeSql).FirstOrDefault();
        }
    }
}
